Skip to content

Themes accept header font family #5887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 11, 2024
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5886.

Briefly, all themes gain a header_family argument that defaults to base_family. In addition, plot.subtitle and plot.caption no longer inherit from title but the root text element to follow #5886 (comment).

A demonstration:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  labs(
    title = "Fuel efficiency",
    subtitle = "Described for 234 cars from 1999 and 2008.",
    caption = "Source: U.S. Environmental Protection Agency",
    tag = "A"
  ) +
  theme_gray(header_family = "Impact")

Created on 2024-05-10 with reprex v2.1.0

@thomasp85
Copy link
Member

I kind of understand why you want to change the inheritance of subtitle and caption but I'm afraid it would throw users off (especially for subtitle). I now regret we didn't name it "description" or something like that

@teunbrand
Copy link
Collaborator Author

So should we revert the change in inheritance and manually set the header font to the relevant elements in theme_*() functions?

@thomasp85
Copy link
Member

I'm not sure, but I think it would be best at least for subtitle. I'm fine with caption not inheriting from title, I think that was a wrong choice from the start

@tidyverse tidyverse deleted a comment from teunbrand May 21, 2024
@teunbrand
Copy link
Collaborator Author

teunbrand commented May 21, 2024

After some thought, I think it would be best to have the header_family = NULL be the default.
That way, you don't bake in header_family = "", which is convenient when you're looking to globally change the text font.

Example with the current PR, notice the titles remaining sans serif:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  labs(
    title = "Fuel efficiency",
    subtitle = "Described for 234 cars from 1999 and 2008.",
    caption = "Source: U.S. Environmental Protection Agency",
    tag = "A"
  ) +
  theme_gray()

p + theme_gray() + 
  theme(text = element_text(family = "Times New Roman"))

However, this all resolves nicely when the header font is NULL, notice the all serif text:

p + theme_gray(header_family = NULL) +
  theme(text = element_text(family = "Times New Roman"))

Created on 2024-05-21 with reprex v2.1.0

teunbrand and others added 4 commits May 21, 2024 14:37
Merge branch 'main' into header_family

# Conflicts:
#	R/theme-defaults.R
#	tests/testthat/test-theme.R
@thomasp85
Copy link
Member

I'm not sure tags should inherit from title... Usually you want these pretty unobtrusive and subdued

@teunbrand
Copy link
Collaborator Author

It also isn't immediately obvious to me why tags should have title style. I'm happy to swap their inheritance to the root text element if you agree.

@thomasp85
Copy link
Member

yeah, let's change that

@teunbrand
Copy link
Collaborator Author

Alright, it now looks like so:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  labs(
    title = "Fuel efficiency",
    subtitle = "Described for 234 cars from 1999 and 2008.",
    caption = "Source: U.S. Environmental Protection Agency",
    tag = "A"
  ) +
  theme_gray(header_family = "Impact")

Created on 2024-07-11 with reprex v2.1.0

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 096b966 into tidyverse:main Jul 11, 2024
11 checks passed
@teunbrand teunbrand deleted the header_family branch July 11, 2024 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Have header font be part of theme specification
2 participants